Properties {
	$build_dir = Split-Path $psake.build_script_file
	$output_dir = "$build_dir\Build\"
	$solution_path = "$build_dir\Duhking.sln"
	$tools_dir = "$build_dir\Tools\"
	$source_dir = "$build_dir\Duhking\"
	$debug_dir = $source_dir + "bin\debug\"
	$release_dir = $source_dir + "bin\release\"
	$package_dir = $output_dir + "package\"
	$lib_dir = $package_dir + "lib\"
}

Task default -Depends Rebuild

Task Build -Depends Compile,ILMerge,ZipFiles

Task ReBuild -Depends Clean,Compile,ILMerge,ZipFiles

Task Compile  {
	Write-Host "Building Duhking solution"
	Exec { msbuild $solution_path /t:Build /p:Configuration=Debug /v:quiet }
}

Task Clean {
	Write-Host "Creating output directory"
	if(Test-Path $output_dir)
	{
		rd $output_dir -Recurse -Force | Out-Null
	}
	mkdir $output_dir | Out-Null
	
	Write-Host "Running Clean..."
	Exec { msbuild $solution_path /t:Clean /p:Configuration=Debug /v:quiet }
}

Task ILMerge {
	Write-Host "Merging assemblies for distribution"
	ilmerge ($debug_dir,$output_dir)
}

Task ZipFiles {
	Write-Host "Zipping files for distribution"
	Exec { tools/zipper "Build\Duhking.*" }
	mv $build_dir\Duhking.zip $output_dir
}

Task Package {
	Write-Host "Packaging for NuGet"
	if(! (Test-Path $package_dir))
	{
		mkdir $package_dir | Out-Null
	}
	if(! (Test-Path $lib_dir))
	{
		mkdir $lib_dir | Out-Null
	}
	cp $output_dir\Duhking.dll $lib_dir
	cp ./NuSpec.xml $package_dir\duhking.nuspec
	Exec { tools\nuget.exe pack $package_dir\duhking.nuspec }
}

function ilmerge()
{
	#Create a new temp directory as we cannot overwrite the main assembly being merged
	
	if($framework -eq "4.0")
	{
		Exec { tools\ilmerge.exe /out:"$output_dir\Duhking.dll" "$debug_dir\Duhking.dll" "$debug_dir\Castle.Core.dll" "$debug_dir\Castle.DynamicProxy2.dll" /v4 }
	}
	else
	{
		Exec { tools\ilmerge.exe /out:"$output_dir\Duhking.dll" "$debug_dir\Duhking.dll" "$debug_dir\Castle.Core.dll" "$debug_dir\Castle.DynamicProxy2.dll" }
	}
}

